package com.qiaoanju.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qiaoanju.domain.UserDomain;
import com.qiaoanju.feign.UserFeignClientByFallbackFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user_hystrix")
public class UserConsumerHystrixController {

    @Autowired
    private UserFeignClientByFallbackFactory userFeignClientByFallbackFactory;

    /**
     * @HystrixCommand 和 fallbackFactory同时设置时优先使用fallbackFactory设置
     * @param id
     * @return
     */
    @GetMapping("/movie/{id}")
    @HystrixCommand(fallbackMethod = "findByIdFallback")
    public UserDomain findById(@PathVariable Long id) {
        return this.userFeignClientByFallbackFactory.findById(id);
    }


    /**
     *
     * 断路默认方法
     * @param id
     * @return
     */
    public UserDomain findByIdFallback(Long id) {
        UserDomain user = new UserDomain();
        user.setId(0L);
        user.setUsername("use HystrixCommand");
        user.setPassword("HystrixCommand");
        return user;
    }

    @PostMapping("/test")
    public UserDomain testPost(UserDomain user) {
        return this.userFeignClientByFallbackFactory.postUser(user);
    }

    @GetMapping("/login")
    public UserDomain login(@RequestParam("username") String username, @RequestParam("password") String password) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", "张三");
        map.put("password", "123");
        return this.userFeignClientByFallbackFactory.login(map);
    }
}
